<?php
class ControllerStartupSass extends Controller {
    /**
     * @throws Exception
     */
    public function index() {
		$dist = DIR_APPLICATION . 'view/stylesheet/bootstrap.css';
		$source = DIR_APPLICATION . 'view/sass/bootstrap.css';

        $need_compile = ! is_file($dist) || !$this->config->get('developer_sass');
        $has_source = is_file($source);

		if ($need_compile && $has_source) {
			$scss = new \Leafo\ScssPhp\Compiler();
			$scss->setImportPaths(DIR_APPLICATION . 'view/stylesheet/sass/');

			$output = $scss->compile('@import "_bootstrap.scss"');
			$fd_dist = fopen($dist, 'w');
			if ($fd_dist) {
                flock($fd_dist, LOCK_EX);

                fwrite($fd_dist, $output);

                fflush($fd_dist);

                flock($fd_dist, LOCK_UN);

                fclose($fd_dist);
            } else {
			    throw new Exception("could not open file $dist");
            }
		}
	}
}
